﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CommandDeleteMarks : HYNoTranExternalCommand
	{
		public override void FilterProcess()
		{
            base.FilterViewType.Add((ViewType)4);
			base.FilterProcess();
		}

		public override Result HYExecute()
		{
			FormDeleteMarks formDeleteMarks = new FormDeleteMarks();
			if (CommandDeleteMarks.hWndRevit == null)
			{
				CommandDeleteMarks.hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			formDeleteMarks.Show(CommandDeleteMarks.hWndRevit);
			Transaction transaction = new Transaction(base.Doc, "删除标注");
			Result result;
			for (;;)
			{
				try
				{
					ICollection<Element> collection = base.Sel.PickElementsByRectangle("请框选删除范围");
					if (collection == null)
					{
						continue;
					}
					if (collection.Count < 1)
					{
						continue;
					}
					ICollection<ElementId> collection2 = (from ele in collection
					select ele.Id).ToList<ElementId>();
					transaction.Start();
					DeleteMarksHelper.DeleteFilterGroup(formDeleteMarks.ListTypes, base.Doc, collection2);
					DeleteMarksHelper.DeleteFilterTags(formDeleteMarks.ListTypes, base.Doc, collection2);
					transaction.Commit();
					continue;
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
                    if ((int)transaction.GetStatus() == 1)
					{
						transaction.Commit();
					}
					formDeleteMarks.Close();
					formDeleteMarks.Dispose();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
				catch (Exception ex)
				{
                    if ((int)transaction.GetStatus() != 3)
					{
						transaction.RollBack();
					}
					formDeleteMarks.Close();
					MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
					result = Autodesk.Revit.UI.Result.Failed;
				}
				break;
			}
			return result;
		}

		public static YJKWindowHandle hWndRevit;
	}
}
